//
//  NSString+GL.m
//  GLLibDemo
//
//  Created by huang long on 17/4/26.
//  Copyright © 2017年 历山大亚. All rights reserved.
//

#import "NSString+GL.h"

@implementation NSString (GL)

+ (instancetype)stringWithDictionary:(NSDictionary *)dict{
    NSError *parseError;
    NSData *jsonData = [NSJSONSerialization dataWithJSONObject:dict options:NSJSONWritingPrettyPrinted error:&parseError];
    if (jsonData && !parseError) {
        return [[NSString alloc] initWithData:jsonData encoding:NSUTF8StringEncoding];
    }
    return nil;
}

- (id)jsonKeyValues{
    NSData *data = [self dataUsingEncoding:NSUTF8StringEncoding];
    if (data) {
        NSError *serializationError;
        id m = [NSJSONSerialization JSONObjectWithData:data options:0 error:&serializationError];
        if (m && serializationError == nil) {
            return m;
        }
    }
    return nil;
}

- (NSData *)dataObject{
    NSError *parseError = nil;
    
    NSData *jsonData = [NSJSONSerialization dataWithJSONObject:self options:NSJSONWritingPrettyPrinted error:&parseError];
    return jsonData;
}

#pragma mark - color
- (UIColor *)toColor{
    if (self == nil) {
        return nil;
    }
    unsigned int hexInt = 0;
    // Create scanner
    NSScanner *scanner = [NSScanner scannerWithString:self];
    // Tell scanner to skip the # character
    [scanner setCharactersToBeSkipped:[NSCharacterSet characterSetWithCharactersInString:@"#"]];
    // Scan hex value
    [scanner scanHexInt:&hexInt];
    
    // Create color object, specifying alpha as well
    UIColor *color = [UIColor colorWithRed:((CGFloat) ((hexInt & 0xFF0000) >> 16))/255
                                     green:((CGFloat) ((hexInt & 0xFF00) >> 8))/255
                                      blue:((CGFloat) (hexInt & 0xFF))/255
                                     alpha:1];
    
    return color;
}

- (UIColor *((^)(CGFloat)))toFullColor{
    return ^(CGFloat alpha){
        return [[self toColor] colorWithAlphaComponent:alpha];
    };
}

#pragma mark - 
- (NSString *((^)(NSInteger point)))toFloat{
    return ^(NSInteger point){
        NSString *format = [NSString stringWithFormat:@"%%.%ldf",point];
        return [NSString stringWithFormat:format,self.floatValue];
    };
}

#pragma mark - byte
- (NSInteger)byteLength{
    
    NSInteger count = 0;
    for (int i = 0; i < self.length; i ++) {
        NSString *tempStr = [self substringWithRange:NSMakeRange(i, 1)];
        const char * cString = [tempStr cStringUsingEncoding:NSUnicodeStringEncoding];
        NSInteger l = strlen(cString);
        if (l == 0 && tempStr.length > 0) {
            const char * tempCStr = [tempStr cStringUsingEncoding:NSUTF8StringEncoding];
            if (tempCStr != NULL) {
                if (strlen(tempCStr) > 2) {
                    count += 2;
                }else{
                    count += strlen(tempCStr);
                }
            }
        }else{
            count += l;
        }
    }
    return count;
}

- (NSInteger)indexWithMaxByteLength:(NSInteger )maxByteLength{
    NSInteger index = 0;
    NSInteger count = 0;
    for (int i = 0; i < self.length; i ++) {
        NSString *tempStr = [self substringWithRange:NSMakeRange(i, 1)];
        const char * cString = [tempStr cStringUsingEncoding:NSUnicodeStringEncoding];
        NSInteger l = strlen(cString);
        if (l == 0 && tempStr.length > 0) { //
            const char * tempCStr = [tempStr cStringUsingEncoding:NSUTF8StringEncoding];
            if (tempCStr != NULL) {
                if (strlen(tempCStr) > 2) {
                    count += 2;
                }else{
                    count += strlen(tempCStr);
                }
            }
        }else{
            count += l;
        }
        if (count > maxByteLength) {
            index = i;
            break;
        }
    }
    return index;
}

- (CGSize)sizeWithFont:(UIFont *)font width:(CGFloat)width{
    NSDictionary * tdic = [NSDictionary dictionaryWithObjectsAndKeys:font,NSFontAttributeName,nil];
    //IOS7 fix: get size that text needs, limit the width
    return  [self boundingRectWithSize:CGSizeMake(width, MAXFLOAT) options:(NSStringDrawingUsesLineFragmentOrigin|NSStringDrawingUsesFontLeading)  attributes:tdic context:nil].size;
}

@end
